package guess;

import java.util.Scanner;

/**
 * @classname:Game
 * @description:
 * @author: Mr.Z
 * @date: 2020/11/27 19:37
 * @version: 0.1
 * @since:1.8
 */
public class Game {
    private static Computer computer;
    private static User user;
    private static Scanner input;
    private static int count;

    static {
        input = new Scanner(System.in);
        computer = new Computer();
        user = new User();
    }

    public static void play() {
        initial();
        System.out.println("是否开启本轮游戏？(y/n)");
        String an = input.next();
        while ("y".equals(an)) {
            startGame();
            result();
        }
    }


    private static void result() {
        System.out.println("---------------------------------------");
        System.out.println(computer.getName() + "     VS    " + user.getName());
        System.out.println("对战次数" + count);
        System.out.println();
        System.out.println("姓名\t\t" + "得分");
        System.out.println(user.getName() + "\t\t" + computer.getName());
        System.out.println(user.getScore() + "\t\t" + computer.getScore());
        System.out.println();
        if (user.getScore() > computer.getScore()) {
            System.out.println("恭喜您赢得了本轮比赛");
        } else if (computer.getScore() > user.getScore()) {
            System.out.println("很不幸电脑赢得了本轮比赛");
        } else {
            System.out.println("平局");
        }
        System.out.println("---------------------------------------");

    }


    private static void startGame() {

        String answer;
        do {
            count++;
            int i = user.showFist(input);
            int i1 = computer.showFist();
            whowin(i, i1);
            System.out.println("是否开启下一轮(y/n)");
            answer = input.next();
        } while ("y".equals(answer));

    }

    //1.剪刀 2.石头 3.布
    private static void whowin(int i, int i1) {
        if (i == 1 && i1 == 2 || i == 2 && i1 == 3 || i == 3 && i1 == 1) {
            System.out.println("电脑赢了");
            int score1 = computer.getScore();
            computer.setScore(score1 + 1);
        } else if (i1 == 1 && i == 2 || i1 == 2 && i == 3 || i1 == 3 && i == 1) {
            System.out.println("您赢了");
            int score = user.getScore();
            user.setScore(score + 1);
        } else {
            System.out.println("和局");
        }
    }


    private static void initial() {
        System.out.println("==============================");
        System.out.println("           猜拳游戏            ");
        System.out.println("=============start============");
        System.out.println("==============================");

        System.out.println("出拳规则：1.剪刀 2.石头 3.布");
        System.out.println("请选择对方角色");
        System.out.println("1.刘备 2.孙权 3.曹操");
        int computerName = input.nextInt();
        if (computerName == 1) {
            computer.setName("刘备");
        } else if (computerName == 2) {
            computer.setName("孙权");
        } else {
            computer.setName("曹操");
        }
        System.out.println("请输入您的姓名");
        String userName = input.next();
        user.setName(userName);
        System.out.println(user.getName() + "    VS    " + computer.getName());
    }
}
